package com.tsing.mapper.impl;

import com.tsing.common.framwork.mapper.CommonDao;
import com.tsing.mapper.UserMapper;
import com.tsing.pojo.entity.AdUser;
import org.apache.ibatis.jdbc.SQL;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class UserMapperImpl extends CommonDao implements UserMapper {

    /**
     * 检索具有指定用户ID的AdUser。
     *
     * @param id 要检索的用户的ID
     * @return 具有指定ID的AdUser对象
     * @throws SQLException 如果执行查询时发生错误
     */
    public AdUser getById(Long id) throws SQLException {
        List<AdUser> result = executeQuery(
                AdUser.class,
                "select * from ad_user where id = ?",
                new Object[]{id});
        if (!result.isEmpty()) {
            return result.get(0);
        } else {
            return null;  // or throw an exception if you prefer
        }
    }

    @Override
    public void add(AdUser adUser) throws SQLException {
        Object[] params = {adUser.getName(),
                adUser.getPassword(),
                adUser.getSalt(),
                adUser.getNickname(),
                adUser.getImage(),
                adUser.getPhone(),
                adUser.getStatus(),
                adUser.getEmail(),
                adUser.getLoginTime(),
                adUser.getCreatedTime()
        };
        executeUpdate("insert into ad_user (name, password, salt, nickname, image, phone, status, email, login_time, created_time)\n" +
                "values (?,?,?,?,?,?,?,?,?,?);", params);
    }

    public List<AdUser> list() throws SQLException {
        return executeQuery(AdUser.class, "select * from ad_user", null);
    }
}
